package com.donn.securitycontrolplatform.equipment.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.db1.mapper.TGroupMapper;
import com.donn.securitycontrolplatform.equipment.entity.TEquipment;
import com.donn.securitycontrolplatform.equipment.entity.TGroup;
import com.donn.securitycontrolplatform.equipment.service.ITGroupService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhenglx
 * @since 2022-06-19
 */
@Service
public class TGroupServiceImpl extends ServiceImpl<TGroupMapper, TGroup> implements ITGroupService {

    @Override
    public Result selectGroupList(TGroup group, Page page) {
        try {
            QueryWrapper<TGroup> queryWrapper = new QueryWrapper<>();
            if (StringUtil.isNotEmpty(group.getGroupName())) {
                queryWrapper.lambda().eq(TGroup::getGroupName, group.getGroupName());
            }
            IPage<TEquipment> equipmentIPage = page(page, queryWrapper);
            return Result.success(equipmentIPage);
        } catch (Exception e) {
            log.error("query error", e);
            return Result.init(500, "service error", e.getMessage());
        }    }


    @Override
    public Result getGroupById(String groupId) {
        try {
            return Result.success(getById(groupId));
        } catch (Exception e) {
            log.error("An error occurred in the getGroupById : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }


    @Override
    public Result deleteGroup(String groupId) {
        try {
            return Result.success(removeById(groupId));
        } catch (Exception e) {
            log.error("An error occurred in the deleteGroup : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    @Override
    public Result updateGroup(TGroup group) {
        try {
            return Result.success(saveOrUpdate(group));
        } catch (Exception e) {
            log.error("An error occurred in the updateGroup : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    @Override
    public Result insertGroup(TGroup group) {
        try {
            return Result.success(saveOrUpdate(group));
        } catch (Exception e) {
            log.error("An error occurred in the insertGroup : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }
}
